package com.zsr.example.controller.front;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zsr.example.config.OssConfigProperties;
import com.zsr.example.config.annotation.OptLog;
import com.zsr.example.config.exception.CommonJsonException;
import com.zsr.example.entity.UcenterMember;
import com.zsr.example.service.EduCollectService;
import com.zsr.example.service.OssService;
import com.zsr.example.service.RedisService;
import com.zsr.example.service.UcenterMemberService;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.util.JwtUtil;
import com.zsr.example.util.MD5;
import com.zsr.example.util.constants.ErrorEnum;
import com.zsr.example.vo.MemberReq;
import com.zsr.example.vo.front.LoginVo;
import com.zsr.example.vo.front.RegisterVo;
import com.zsr.example.vo.front.UserInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import static com.zsr.example.util.constants.SysLogConstants.UPLOAD;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2021-12-04
 */
@RestController
@CrossOrigin
@RequestMapping("/educenter/ucenter")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;
    @Autowired
    private OssService ossService;
    @Autowired
    private RedisService redisService;

    /**
     * 会员登录
     *
     * @param loginVo
     * @return
     */
    @PostMapping("login")
    public JSONObject login(@RequestBody LoginVo loginVo) {
        String token = ucenterMemberService.login(loginVo);
        return CommonUtil.successJson(token);
    }

    /**
     * 会员注册
     *
     * @param registerVo
     * @return
     */
    @PostMapping("register")
    public JSONObject register(@RequestBody RegisterVo registerVo) {
        ucenterMemberService.register(registerVo);
        return CommonUtil.success();
    }

    /**
     * 根据token获取登录信息
     *
     * @param request
     * @return
     */
    @GetMapping("auth/getLoginInfo")
    public JSONObject getLoginInfo(HttpServletRequest request) {
        try {
            String memberId = JwtUtil.getMemberIdByJwtToken(request);
            UserInfoVo loginInfoVo = ucenterMemberService.getLoginInfo(memberId);
            return CommonUtil.successJson(loginInfoVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CommonJsonException(ErrorEnum.E_20001);
        }
    }


    @PostMapping("reset")
    public JSONObject reset(@RequestBody UcenterMember user){
        UpdateWrapper<UcenterMember> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",user.getId());
        if(!StringUtils.isEmpty(user.getNickname()))updateWrapper.set("nickname",user.getNickname());
        if(!StringUtils.isEmpty(user.getAvatar()))updateWrapper.set("avatar",user.getAvatar() );
        if(!StringUtils.isEmpty(user.getAge()))updateWrapper.set("age",user.getAge() );
        if(!StringUtils.isEmpty(user.getSex()))updateWrapper.set("sex",user.getSex() );
        ucenterMemberService.update(user,updateWrapper);
        return CommonUtil.successJson();
    }

    @PostMapping("changPassword")
    public JSONObject changPassword(HttpServletRequest request,@RequestBody RegisterVo user){
        String id = JwtUtil.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(id))throw new CommonJsonException(ErrorEnum.E_20011);
        if(StringUtils.isEmpty(user.getEmail()))throw new CommonJsonException("20001","请输入邮箱");
        if(StringUtils.isEmpty(user.getCode()))throw new CommonJsonException("20001","请输入验证码");
        if(StringUtils.isEmpty(user.getPassword()))throw new CommonJsonException("20001","请输入密码");
        if(StringUtils.isEmpty(user.getPwd2()))throw new CommonJsonException("20001","请输入确认密码");
        if(!user.getPwd2().equals(user.getPassword()))throw new CommonJsonException("20001","确认密码不一致");

        String mobleCode = redisService.opsForValueGet("change"+user.getEmail());
        if (!user.getCode().equals(mobleCode)) throw new CommonJsonException("20001", "验证码错误");

        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(user, ucenterMember);
        ucenterMember.setId(id);
        ucenterMember.setPassword(MD5.encrypt(user.getPassword()));
        ucenterMemberService.updateById(ucenterMember);
        return CommonUtil.success();
    }

    /**
     * 上传头像的方法
     * @param file
     * @param host
     * @return
     */
    @OptLog(optType = UPLOAD,optDesc = "上传文件")
    @PostMapping("fileoss")
    public JSONObject uploadOssFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "host", required = false) String host) {
        if(!StringUtils.isEmpty(host)){
            OssConfigProperties.FILE_HOST = host;
        }
        String url = ossService.uploadFileAvatar(file,host);
        return CommonUtil.successJson(url);
    }
}

